package com.generic.action;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.hibernate.HibernateException;

import com.generic.ds.HibernateSession;
import com.generic.handler.impl.MemberHandler;
import com.generic.handler.impl.SellerHandler;
import com.generic.module.impl.Member;
import com.generic.module.impl.Seller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;

@InterceptorRef("jsonValidationWorkflowStack")
public class SignIn extends ActionSupport {
	
	private static final long serialVersionUID = -7415901766283800818L;
	private String email = null;
	private String password = null;
	private String type = null;
	
	public String getEmail() {
		return email;
	}

	public String getPassword() {
		return password;
	}
	
	public String getType() {
		return type;
	}
	
	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@EmailValidator(key = "error.invalid")
	public void setEmail(String email) {
		this.email = email;
	}

	@RequiredStringValidator(key = "error.required", shortCircuit = true)
	@StringLengthFieldValidator(key = "error.length", trim = true, minLength = "6",  maxLength = "20")
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	@SkipValidation
	public String execute() {
		return SUCCESS;
	}
	
	@Action(value="/sign-in-authenticate",results={
			@Result(name="input",location="ajax-msg.jsp"),
			@Result(name="success", location="ajax-msg.jsp")})
	public String authenticate() throws Exception {
		String status = SUCCESS;
		HibernateSession hs = new HibernateSession();
		try {
			hs.beginTransaction();
			Map<String, Object> session = ActionContext.getContext().getSession();
			
			if(type.equals("buyer")) {
				MemberHandler dao = new MemberHandler(hs);
				Member member = (Member)dao.get("email", email);
				if(member != null && password.equals(member.getPassword())) {
					session.put("BUYER", member);
				} else {					
					addActionError(getText("error.failed"));
					status = INPUT;
				}
			} else {
				SellerHandler dao = new SellerHandler(hs);
				Seller seller = (Seller)dao.get("email", email);
				if(seller != null && password.equals(seller.getPassword())){
					session.put("SELLER", seller);
				} else {
					addActionError(getText("error.failed"));
					status = INPUT;
				}
			}
			
			hs.commitTransaction();
		} catch (HibernateException e) {
			hs.rollbackTransaction(e);
		} finally {
			hs.closeSession();
		}
		
		return status;
	}
}